import { CoolController, BaseController } from '@cool-midway/core';
import { CmsArticleEntity } from '../../entity/article';
import { CmsCategoryEntity } from '../../entity/category';
import { CmsArticleService } from '../../service/article';
import { Get, Inject, Query } from '@midwayjs/decorator';

/**
 * cms模块-文章管理
 */
@CoolController({
  api: ['info', 'page'],
  entity: CmsArticleEntity,
  service: CmsArticleService,
  pageQueryOp: {
    fieldEq: ['category_id', 'is_sticky', 'status'],
    select: ['a.*', 'b.categoryname'],
    join: [
      {
        entity: CmsCategoryEntity,
        alias: 'b',
        condition: 'a.category_id = b.id',
        type: 'leftJoin',
      },
    ],
  },
  listQueryOp: {
    fieldEq: ['status', 'category_id'],
  },
})
export class CmsAppArticleController extends BaseController {
  @Inject()
  cmsArticleService: CmsArticleService;
  /**
   * 根据文章id获取上一篇/下一篇文章
   */
  @Get('/around', { summary: '根据文章id获取上一篇/下一篇文章' })
  async around(@Query('id') id: Number) {
    const result = await this.cmsArticleService.getAroundById(id);
    return this.ok(result);
  }
}